//
//  SYCollectionTableView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/12.
//  Copyright © 2019年 李文强. All rights reserved.
//

#import "SYCollectionTableView.h"
#import "SYCollectionTableViewCell.h"
#import "SYPPTViewController.h"
#import "SYNewAlerView.h"
#import "SYMobileView.h"
#import "SYPlayerViewController.h"
#import "GFWebViewController.h"

@interface SYCollectionTableView () <UITableViewDelegate,UITableViewDataSource,SYCollectionTableViewCellDelegate>

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *dataArr;

@property (strong, nonatomic) UIViewController *contVC;

@property (strong, nonatomic) SYNewAlerView *alerView;
@property (strong, nonatomic) SYMobileView *mobileView;
@property (strong, nonatomic) SYCollectionModel *selectModel;//选中需要操作的model
@end


@implementation SYCollectionTableView


- (instancetype)init {
    self = [super init];
    if (self) {
        self.dataArr = [NSMutableArray array];
    }
    return self;
}

- (void)initSubView {
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.equalTo(self);
    }];
    [self.tableView reloadData];
}



- (void)subUpViewArr:(NSMutableArray *)dataArr vc:(UIViewController *)vc WithIsFrom:(NSString *)isFrom {
    
    self.isFrom = isFrom;

    self.contVC = vc;
    self.dataArr = dataArr;
    [self initSubView];
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"SYCollectionTableViewCell" bundle:nil] forCellReuseIdentifier:@"SYCollectionTableViewCell"];
        
    }
    return _tableView;
}

#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYCollectionTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"SYCollectionTableViewCell" forIndexPath:indexPath];
    tableCell.collectionModel = self.dataArr[indexPath.row];
    tableCell.type = self.stats;
    [tableCell functionTableModel:self.dataArr[indexPath.row] indexPath:indexPath];
    tableCell.delegate = self;
    return tableCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (self.stats == SYCollectionViewTypeWeiKe) {//微课
//        SYCollectionModel *model = self.dataArr[indexPath.row];
//        SYCollectionViewType type = -1;
//        if (self.stats !=0) {//传入过来的数据
//            type = self.stats;
//        }
//
//        if ([model.type isEqualToString:@"5"]) {//微课程
//            type = SYCollectionViewTypeWeiKe;
//        }
//        if (self.block) {//会掉刷新页面
//            self.block(type, nil);
//        }
//    } else {
//        SYPPTViewController *pptVC = [[SYPPTViewController alloc] init];
//        pptVC.view.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
//         pptVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
//        [self.contVC presentViewController:pptVC animated:YES completion:nil];
//    }
    SYCollectionModel *model = self.dataArr[indexPath.row];
    if ([model.type isEqualToString:@"3"]) {//视频
        SYPlayerViewController *playerVC = [[SYPlayerViewController alloc] init];
        playerVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        playerVC.urlStr = model.path;
        [self.contVC presentViewController:playerVC animated:YES completion:nil];
        return;
    }
    SYCollectionViewType type = -1;
    if (self.stats !=0) {//传入过来的数据
        type = self.stats;
    }
    
    if ([model.type isEqualToString:@"5"]) {//微课程
        type = SYCollectionViewTypeWeiKe;
    } else if ([model.type isEqualToString:@"1"]) {//新建文件夹
        type = SYCollectionViewTypeNewAddWenJianJia;
    }else if ([model.type isEqualToString:@"9"]) {//新建文件夹
            
    //        type = SYCollectionViewTypeNewAddWenJianJia;
            GFWebViewController *vc = [[GFWebViewController alloc]init];
            vc.urlStr = model.path;
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [self.contVC presentViewController:vc animated:YES completion:nil];
            return;

            
        } else if ([model.type isEqualToString:@"4"] || [model.type isEqualToString:@"6"] ||[model.type isEqualToString:@"7"] || [model.type isEqualToString:@"8"]) { //ppt word excel 图片
        
        SYPPTViewController *wordVC = [[SYPPTViewController alloc] init];
        wordVC.urlStr = model.path;
        wordVC.nameStr = model.titleName;
        wordVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self.contVC presentViewController:wordVC animated:YES completion:nil];
        return;
    }
    
    if (self.block) {
        self.block(type, model);
    }
    
}

#pragma mark - sycollectiondele
- (void)selectTableLogPressType:(SYFunctionCollectionType)type indexPath:(NSIndexPath *)index {
    SYCollectionModel *model = self.dataArr[index.row];
    _selectModel = model;
    
    
    
    __weak typeof(self) weakSelf = self;
    switch (type) {
        case SYFunctionCollectionTypeDelete://删除
        {
            [self requestDelete];
            
        }
            break;
        case SYFunctionCollectionTypeMobile://移动
        {
            
            [self yiDongWeiZhiQuFen];
            
        }
            break;
        case SYFunctionCollectionTypeRename://重命名
        {
            
            _alerView = [[SYNewAlerView alloc] init];
            [_alerView showAler:_alerView titleName:@"重命名" textFielPlo:@"新名字"];
            _alerView.block = ^(NSString *titleName) {//先这样写着。到时候在更改
                [weakSelf requestRenameFile:titleName collectionModel:model];
                
            };
        }
            break;
        default:
            break;
    }
}
//重命名
- (void)requestRenameFile:(NSString *)titleName collectionModel:(SYCollectionModel *)model {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    NSString *onlySort = nil;
    if ([model.type isEqualToString:@"1"]) {//文件夹
        onlySort = @"1";
    } else {
        onlySort = @"0";
    }
    
    [SYRequestAPI zy_requestMyRenameFileFileName:titleName wjId:model.zyid onlySort:onlySort success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:@"命名成功"];
        [NSObject executeRunloop:^{
            if (weakSelf.block) {
                weakSelf.block(SYCollectionViewTypeAll, nil);
            }
        } afterDelay:1];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
}

#pragma mark - 移动的方法
//移动
//移动位置区分 收藏，微课，文件
- (void)yiDongWeiZhiQuFen {
    if ([self.titleName isEqualToString:@"我的收藏"]) {
        [self requestWoDeShouCang];
    } else if ([self.titleName isEqualToString:@"我的作品"]) {
        [self requestWoDeWenJianYiDong];
    }
    
    
}



//我的收藏文件移动
- (void)requestWoDeShouCang {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    if (self.typePid.length == 0) {
        self.typePid = @"0";
    }
    //查询文件夹
    [SYRequestAPI zy_resourceCollectionCollectionListPageIndex:0 pageSize:1000 sortPid:self.typePid isTinyClass:@"0" onlySort:@"1" success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:weakSelf];
        
        [weakSelf shuJuQingQiuChengGongData:listArr];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:weakSelf];
        [MBProgressHUD zr_showError:error.message];
    }];
    
}
//我的文件移动
- (void)requestWoDeWenJianYiDong {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    //查询文件夹
    [SYRequestAPI zy_requestMyResourceListPageIndex:0 pageSize:1000 sortId:@"0" isTinyClass:@"0" onlySort:@"1" keyWord:@"" success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:weakSelf];
        
        [weakSelf shuJuQingQiuChengGongData:listArr];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:weakSelf];
        [MBProgressHUD zr_showError:error.message];
    }];
}




//数据请求成功需要添加战士的类
- (void)shuJuQingQiuChengGongData:(NSMutableArray *)listArr {
    self.mobileView = [[SYMobileView alloc] init];
    self.selectModel.zongFileName = self.titleName;
    [self.mobileView showAler:self.mobileView collectinModel:self.selectModel dataListArr:listArr];
    __weak typeof(self) weakSelf = self;
    //移动成功 刷新数据
    self.mobileView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(SYCollectionViewTypeAll, nil);
        }
    };
}
#pragma mark - 删除
//删除
- (void)requestDelete {
    if ([self.titleName isEqualToString:@"我的收藏"]) {
        [self requestWoDeShouCangDelete];
    } else if ([self.titleName isEqualToString:@"我的作品"]) {
        [self requestWoDeWenJianDelete];
    }
}

//我的收藏
- (void)requestWoDeShouCangDelete {
    [MBProgressHUD mj_showMessage:nil toView:self];
    NSString *onlySort = nil;
    if ([_selectModel.type isEqualToString:@"1"]) {//文件夹
        onlySort = @"1";
    } else {
        onlySort = @"0";
    }
    __weak typeof(self) weakSelf = self;
    NSString *stant = _selectModel.zyid;
    //删除成功会掉接口刷新
    [SYRequestAPI zy_resourceCollectionRemoveCollectionid:stant onlySort:onlySort success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self];
        if (isSuccess) {
            [MBProgressHUD zr_showError:@"删除成功"];
            //删除成功会掉接口
            if (weakSelf.block) {
                weakSelf.block(SYCollectionViewTypeAll, nil);
            }
        }
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
}

//我的文件
- (void)requestWoDeWenJianDelete {
    
    [MBProgressHUD mj_showMessage:nil toView:self];
    NSString *onlySort = nil;
    if ([_selectModel.type isEqualToString:@"1"]) {//文件夹
        onlySort = @"1";
    } else {
        onlySort = @"0";
    }
    __weak typeof(self) weakSelf = self;
    NSString *stant = _selectModel.zyid;
    
    [SYRequestAPI zy_requestMyDeleteFileId:stant onlySort:onlySort success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self];
        if (isSuccess) {
            [MBProgressHUD zr_showError:@"删除成功"];
            //删除成功会掉接口
            if (weakSelf.block) {
                weakSelf.block(SYCollectionViewTypeAll, nil);
            }
        }
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
    
    
    
}
@end












